package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/extCproduct/")
public class ExtCproductController extends BaseController {
@Reference
private FactoryService factoryService;
@Reference
private ContractProductService contractProductService;
@Reference
private ExtCproductService extCproductService;
    @Autowired
    private FileUploadUtil fileUploadUtil;
    @RequestMapping(value = "/list",name ="展示购销合同货物下附件列表数据")
    public String list(String contractId,String contractProductId,@RequestParam(name = "page",defaultValue = "1")int pageNum, @RequestParam(name = "pageSize",defaultValue = "5")int pageSize){
        //
//        需要向request域中放4个数据：1、用来生成附件的工厂  2、这个货物下的所有附件数据 3、货物id 4、合同id

//         查询用来生成货物的工厂数据 select * from co_factory where ctype='附件'
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

//        查询此货物下的所有附件数据  select * from 附件表 where 货物id=？
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo pageInfo = extCproductService.findAll(extCproductExample, pageNum, pageSize);
        request.setAttribute("page",pageInfo);
//       3、 货物id
        request.setAttribute("contractProductId",contractProductId);
        request.setAttribute("contractId",contractId);
        return "cargo/extc/extc-list";
    }

    @RequestMapping(value = "/edit",name ="添加购销合同货物下附件")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto){
        String upload=null;
        try {
            if (productPhoto!=null){
                upload=fileUploadUtil.upload(productPhoto);
            }
        } catch (Exception e) {
            upload=null;
            System.out.println(e.getMessage());
        }
        extCproduct.setProductImage(upload);
        if (StringUtils.isEmpty(extCproduct.getId())){
            extCproduct.setId(UUID.randomUUID().toString());
            extCproduct.setCompanyId(getCompanyId());
            extCproduct.setCompanyName(getCompanyName());
            extCproduct.setCreateTime(new Date());
            extCproductService.save(extCproduct);
        }else {
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }
    @RequestMapping(value = "/toUpdate",name ="修改购销合同货物下附件")
    public String toUpdate(String id){
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);
//         货物id
        request.setAttribute("contractProductId",extCproduct.getContractProductId());
        //       4、 为了保存货物时关键合同
       request.setAttribute("contractId",extCproduct.getContractId());
       return "cargo/extc/extc-update";
    }

    @RequestMapping(value = "/delete",name = "删除购销合同货物下附件")
    public String delete(String id,String contractId,String contractProductId){
        extCproductService.delete(id); //删除货物
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }

}
